package com.hopu.blb_erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.SCompany;
import com.hopu.blb_erp.service.ISCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxl
 * @since 2021-11-20
 */
@RestController
public class SCompanyController {
    @Autowired
    private ISCompanyService companyService;
    @PostMapping("/company")
    public ResponseEntity<String> add(@RequestBody SCompany company){
        LocalDateTime now = LocalDateTime.now();
        company.setCreatTime(now);
        int l = (int) System.currentTimeMillis();
        company.setCompanyStatus("已认证");
        company.setCompanyId(l);
        companyService.save(company);
        return ResponseEntity.ok("ok");
    }
    @GetMapping("/companys")
    public ResponseEntity<List<SCompany>> list(){
        List<SCompany> list = companyService.list();
        return ResponseEntity.ok(list);
    }

    @GetMapping("/company/like")
    public ResponseEntity<Page<SCompany>> findLikePage(Integer current, Integer pageSize, String province, String city, String area, String name){
        if ("".equals(province) || "null".equals(province)){
            province = null;
        }
        if ("".equals(city) || "null".equals(city)){
            city = null;
        }
        if ("".equals(area) || "null".equals(area)){
            area = null;
        }
        if ("".equals(name) || "null".equals(name)){
            name = null;
        }
        QueryWrapper<SCompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(province), "company_province", province)
                .like(StringUtils.isNotBlank(city), "company_city", city)
                .like(StringUtils.isNotBlank(area), "company_area", area)
                .like(StringUtils.isNotBlank(name), "company_name", name);
        Page<SCompany> sCompanyPage = new Page<>(current,pageSize);
        Page<SCompany> page = companyService.page(sCompanyPage,queryWrapper);
        return ResponseEntity.ok(page);
    }


}
